package com.rc.saas.tenant.service.tenant;

import com.rc.saas.tenant.common.constant.WebConstant;
import com.rc.saas.tenant.model.tenant.TenantMenu;
import com.rc.saas.tenant.model.tenant.TenantMenuExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.utils.BuildTreeUtil;
import com.rc.saas.tenant.vo.tenant.TreeNodeModel;
import com.rc.saas.tenant.vo.tenant.TreeModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sven on 2018-04-22
 */
@Service
public class TenantNavServiceImpl implements TenantNavService {
    @Autowired
    private TenantUserService tenantUserService;

    @Autowired
    private TenantMenuService tenantMenuService;

    //@Override
    //public List<TreeModel<TreeMenu>> listMenuTree(TenantUser user) {
    //    List<TreeModel<TreeMenu>> trees = new ArrayList<TreeModel<TreeMenu>>();
    //
    //    List<TenantMenu> lists;
    //    if (user.getUserType().equals(ShiroConstants.TENANT_ADMIN_TYPE)) {//超级管理员拥有所有权限
    //        TenantMenuExample example = new TenantMenuExample();
    //        //状态 0=显示 1=隐藏
    //        example.createCriteria().andStatusEqualTo("0");
    //        example.setOrderByClause("sort");
    //        lists = tenantMenuService.selectByExample(example);
    //    } else {
    //        lists = tenantMenuService.selectMenuByUser(user.getTenantUserId());
    //    }
    //
    //    //生成菜单树
    //    for (TenantMenu permission : lists) {
    //        TreeModel<TreeMenu> tree = new TreeModel<TreeMenu>();
    //        tree.setId(permission.getTenantMenuId().toString());
    //        tree.setParentId(String.valueOf(permission.getParentId()));
    //        tree.setText(permission.getMenuName());
    //
    //        Map<String, Object> attributes = new HashMap<>(16);
    //        attributes.put("url", permission.getHref());
    //        attributes.put("icon", permission.getIcon());
    //        tree.setAttributes(attributes);
    //        trees.add(tree);
    //    }
    //
    //    // 默认顶级菜单为０，根据数据库实际情况调整
    //    List<TreeModel<TreeMenu>> list = BuildTreeUtil.buildList(trees, "0");
    //    return list;
    //}


    @Override
    public List<TreeNodeModel> listTopMenuTreeByUser(TenantUser tenantUser) {
        List<TreeNodeModel> listVo = new ArrayList<TreeNodeModel>();

        //用户类型（0 运营商 1 分销商 2企业用户）默认0
        List<TenantMenu> lists;

        lists = tenantMenuService.listTopMenuByTenant(tenantUser);
//        if (tenantUser.getUserType().equals(WebConstant.STATUS_ZERO)) {//运营商
//            //TenantMenuExample example = new TenantMenuExample();
//            //example.createCriteria().andParentIdEqualTo(0L).andUserTypeEqualTo("0").andStatusEqualTo("0");
//            //example.setOrderByClause("sort");
//            //lists = tenantMenuService.selectByExample(example);
//
//        }
////        else if (tenantUser.getUserType().equals(WebConstant.STATUS_ONE)) {//分销商
////            //lists = tenantMenuService.selectMenuByUser(user.getTenantUserId());
////            lists = tenantMenuService.listTopMenuByOperator();
////        } else if (tenantUser.getUserType().equals(WebConstant.STATUS_TWO)) {//企业帐户
////            lists = tenantMenuService.listTopMenuByEnterprise();
////        } else if (tenantUser.getUserType().equals(WebConstant.STATUS_THREE)) {
////            lists = tenantMenuService.listTopMenuByTenant(tenantUser);
////        }
//        else {
//            lists = tenantMenuService.listMenuByRole(tenantUser.getTenantRoleId());;
//        }

        //生成菜单列表
        for (TenantMenu menu : lists) {
            TreeNodeModel vo = new TreeNodeModel();
            vo.setMenuId(menu.getTenantMenuId());
            vo.setParentId(menu.getParentId());
            vo.setName(menu.getMenuName());
            vo.setUrl("/welcome2/" + menu.getTenantMenuId());

            listVo.add(vo);
        }

        return listVo;
    }

    @Override
    public List<TreeModel<TreeNodeModel>> listLeftMenuTreeByUser(TenantUser tenantUser, Long topMenuId) {
        List<TreeModel<TreeNodeModel>> trees = new ArrayList<TreeModel<TreeNodeModel>>();

        //用户类型（0 运营商 1 分销商 2企业用户）默认0
        List<TenantMenu> tenantMenus;
        tenantMenus = tenantMenuService.listMenuByTopMenuId(tenantUser, topMenuId);
//        if (tenantUser.getUserType().equals(WebConstant.STATUS_ZERO)) {//运营商
//            //or 条件表达式
//            //TenantMenuExample example = new TenantMenuExample();
//            //example.or()
//            //        .andTenantMenuIdEqualTo(topMenuId);
//            //example.or()
//            //        .andParentIdEqualTo(topMenuId).andUserTypeEqualTo("0").andStatusEqualTo("0");
//            //
//            //example.setOrderByClause("sort");
//            //lists = tenantMenuService.selectByExample(example);
//
//
//        } else if (tenantUser.getUserType().equals(WebConstant.STATUS_ONE)) {//分销商
//            //tenantMenus = tenantMenuService.listOperatoMenuByTopMenuId(topMenuId);
//            tenantMenus = tenantMenuService.listOperatoMenuByTopMenuIdAndOperator(tenantUser.getTenantOperatorCode(), topMenuId);
//        } else if (tenantUser.getUserType().equals(WebConstant.STATUS_TWO)) {//企业帐户
//            tenantMenus = tenantMenuService.listEnterpriseMenuByTopMenuId(topMenuId);
//        } else if (tenantUser.getUserType().equals(WebConstant.STATUS_THREE)) {
//            tenantMenus = tenantMenuService.listMenuByTopMenuId(tenantUser, topMenuId);
//        } else {
//            return null;
//        }

        //生成菜单树
        for (TenantMenu menu : tenantMenus) {
            TreeModel<TreeNodeModel> tree = new TreeModel<TreeNodeModel>();
            tree.setId(menu.getTenantMenuId().toString());
            tree.setParentId(String.valueOf(menu.getParentId()));
            tree.setText(menu.getMenuName());

            Map<String, Object> attributes = new HashMap<>(16);
            if (StringUtils.isNotBlank(menu.getHref())) {
                attributes.put("url", menu.getHref());
            } else {
                attributes.put("url", "/empty");
            }

            attributes.put("icon", menu.getIcon());
            tree.setAttributes(attributes);
            trees.add(tree);
        }

        // 默认顶级菜单为０，根据数据库实际情况调整
        List<TreeModel<TreeNodeModel>> list = BuildTreeUtil.buildList(trees, "0");
        return list;
    }

    @Override
    public TreeModel<TreeNodeModel> buildTree(TenantUser user) {
        List<TreeModel<TreeNodeModel>> trees = new ArrayList<TreeModel<TreeNodeModel>>();
        String userType = user.getUserType();
        //如果用户类型是运营商部门，视为运营商
        if ("0".equals(userType) || "3".equals(userType)) {
            userType = "0";
        }
        TenantMenuExample example = new TenantMenuExample();
        example.createCriteria().andStatusEqualTo("0").andUserTypeEqualTo(userType);
        example.setOrderByClause("sort");
        List<TenantMenu> tenantMenus = tenantMenuService.selectByExample(example);
        for (TenantMenu tenantMenu : tenantMenus) {
            TreeModel<TreeNodeModel> tree = new TreeModel<TreeNodeModel>();

            tree.setId(tenantMenu.getTenantMenuId().toString());
            tree.setParentId(tenantMenu.getParentId().toString());
            tree.setText(tenantMenu.getMenuName());

            //权限类型: 0 目录 1 菜单 2 功能
            if (tenantMenu.getMenuType().equals("2")) {
                tree.setType("menuTypeIcon");
            }

            trees.add(tree);
        }

        // 默认顶级菜单为０，根据数据库实际情况调整
        TreeModel<TreeNodeModel> tree = BuildTreeUtil.build(trees);
        return tree;
    }

    @Override
    public TreeModel<TreeNodeModel> buildTree(TenantUser user, Long roleId) {
        //所有权限
        TenantMenuExample example = new TenantMenuExample();
        //如果用户类型是运营商部门，视为运营商
        String userType = user.getUserType();
        if ("0".equals(userType) || "3".equals(userType)) {
            userType = "0";
        }
        example.createCriteria().andStatusEqualTo("0").andUserTypeEqualTo(userType);
        example.setOrderByClause("sort");
        List<TenantMenu> allMenus = tenantMenuService.selectByExample(example);

        // 根据roleId查询已开通的权限
        List<TenantMenu> MenuVos = tenantMenuService.listMenuByRole(roleId);

        //取菜单ID
        List<Long> menuIds = new ArrayList<>();
        for (TenantMenu tenantMenu : MenuVos) {
            menuIds.add(tenantMenu.getTenantMenuId());
        }

        List<Long> temp = menuIds;

        //显示未开通的菜单
        for (TenantMenu menu : allMenus) {
            if (temp.contains(menu.getParentId())) {
                menuIds.remove(menu.getParentId());
            }
        }

        List<TreeModel<TreeNodeModel>> trees = new ArrayList<TreeModel<TreeNodeModel>>();

        for (TenantMenu tenantMenu : allMenus) {
            TreeModel<TreeNodeModel> tree = new TreeModel<TreeNodeModel>();

            tree.setId(tenantMenu.getTenantMenuId().toString());
            tree.setParentId(tenantMenu.getParentId().toString());
            tree.setText(tenantMenu.getMenuName());

            Map<String, Object> state = new HashMap<>(16);
            Long menuId = tenantMenu.getTenantMenuId();

            //是否已开通
            if (menuIds.contains(menuId)) {
                state.put("selected", true);
            } else {
                state.put("selected", false);
            }
            tree.setState(state);

            //权限类型: 0 目录 1 菜单 2 功能
            if (tenantMenu.getMenuType().equals("2")) {
                tree.setType("menuTypeIcon");
            }

            trees.add(tree);
        }

        // 默认顶级菜单为０，根据数据库实际情况调整
        TreeModel<TreeNodeModel> t = BuildTreeUtil.build(trees);
        return t;
    }

}
